var dates={
    date:new Date(),
    year:function () {
        return this.date.getFullYear();
    },
    month:function () {
        return this.date.getMonth()+1;
    },
    day:function () {
        return this.date.getDate();
    },
    week:function () {
        return this.date.getDay();
    },
    hour:function () {
        return this.date.getHours();
    },
    minutes:function () {
        return this.date.getMinutes();
    },
    seconds:function () {
        return this.date.getSeconds();
    }
};
var oMyselect=$('#myselect');
var oDrop=$('.dropdown-menu');
oDrop.on('click','a',function () {
    var text=$(this).text();
    oMyselect.html(text+'<span class="caret"></span>')
});


var timer=dates.year()+'-'+dates.month()+'-'+dates.day()+' '
    +dates.hour()+':'+dates.minutes();

//所有的公告
var selectNum = 1; //发送到园区/班级的值
var Name = ["李院长:"];//
var name2=["张老师:"];
var yqNotice=JSON.parse(localStorage.getItem('data-1')) || []; //园区公告
var bjNotice=JSON.parse(localStorage.getItem('data-1')) || []; //班级公告
var allNotice=JSON.parse(localStorage.getItem('data-1')) || []; //所有公告
var oContainer=$('#container');
var oDiv1=$('#div1');
var oBtn=$('#btn');

if (allNotice.length){
    add();
}
if (yqNotice.length){
    add();
}
if (bjNotice.length){
    add();
}
var ue = UE.getEditor('container');
var mailReg=/[a-z0-9A-Z][- | a-z0-9A-Z . _]{2,19}@[a-z0-9A-Z]{2,20}(\.[a-z]{2,}){1,2}/;

// 选择发送到园区/班级
$(".dropdown-menu").on("click",'li',function(){
    selectNum = $(this).val()

});
// 发送
$("#btn").on("click",function () {
    var title=$('script[name="content"]').val();

    var content2=ue.getContent();
    // alert(content2);

    if ($.trim(content2)==""){
        alert("输入的内容不能为空");
        return false
    }
    var obj = {
        name: Name,
        name2:name2,
        time: timer,
        content2: content2
    };
    allNotice.push(obj);
    if(selectNum==1){
        yqNotice.push(obj)
    }else if(selectNum==2){
        bjNotice.push(obj)
    }else if (selectNum==3){
        $('#btn').attr('data-target','#myModal');
        }
        add();
});

        $('.footer-send').on('click',function () {
            var oInput=$('#modal-body-input').val();
            if (!mailReg.test(oInput)){
                alert("邮箱格式不正确,请重新输入")
            }else {
                alert('发送成功');
                $('#myModal').modal('hide');
                $('#btn').attr('data-target','');
            }
            });



//添加;
function add() {
    if (selectNum == 1) {
        var str1 = "";
        var str3 = "";
        $.each(yqNotice, function (index, ele) {
            str1 = str3 = '<div class="div5">' +
                '<span>' + ele.name + '</span>' +
                '<span class="del"><i class="fa fa-times" aria-hidden="true"></i></span>'+
                '<p>' + ele.content2 + '</p>' +
                '<div style="text-align: right">' + ele.time + '</div>' +
                '</div>';
            $('.bg').on('click','.del',function () {
                $(this).parent().remove()
            });
        });

        $("#div1").prepend(str1);
        $("#div3").prepend(str3);
        localStorage.setItem('data-1', JSON.stringify(yqNotice));
        localStorage.setItem('data-1', JSON.stringify(allNotice))
    } else if (selectNum == 2) {
        var str2 = "";
        var str4 = "";
        $.each(bjNotice, function (index, ele) {
            str2 = str4 = '<div class="div5">' +
                '<span>' + ele.name2 + '</span>' +
                '<span class="del"><i class="fa fa-times" aria-hidden="true"></i></span>'+
                '<p>' + ele.content2 + '</p>' +
                '<div style="text-align: right;margin-right: 20px">' + ele.time + '</div>' +
                '</div>';
            $('.bg').on('click','.del',function () {
                $(this).parent().remove()
            });
        });
        $("#div2").prepend(str2);
        $("#div3").prepend(str4);
        localStorage.setItem('data-1', JSON.stringify(bjNotice));
        localStorage.setItem('data-1', JSON.stringify(allNotice))
    }
}

